<?php
// 本类由系统自动生成，仅供测试用途
namespace MyClass;
class Inquiry extends \MyClass\Common {
    public function Lists(){
		if($_REQUEST['order']){
			if($_REQUEST['order'] ==1) {
				$ordertype = 'a.Pubtime desc';
			} else{
				$ordertype = 'a.Pubtime asc';
			}
			$this->s("order",$_REQUEST['order']);
		}else{
			$ordertype = 'a.Pubtime desc';
		}
		if($_REQUEST['st']){
			if($_REQUEST['st'] ==1) {
				$endsql = ' AND a.Endtime > '.time();
			} elseif($_REQUEST['st'] ==2){
				$endsql = ' AND a.Endtime <= '.time();
			}else{
				$endsql = 'AND 1=1';
			}
			$this->s("st",$_REQUEST['st']);
		}else{
			$endsql = ' AND 1=1';
		}
		//$count = count($this->m("actinquiry")->where("Ifdel = 1".$endsql.$key)->select());
		//$p = new \Extend\Page($count,15);
		$list = $this->m("actinquiry a")->join("company c on a.Acid = c.Id")->where("Ifdel = 1".$endsql.$key)->field("a.*, c.CompanyName,c.CompanyRegionId")->order($ordertype)->limit(6)->select();
		
		$this->s("list",$list);
		$this->v();
    }
	public function ListShow(){
		if(!isset($_REQUEST['Id'])){
			$this->error("非法操作");
		}
		$Id = $_REQUEST['Id'];
		$act = $this->m("actinquiry")->where("Id = ".$Id." AND Ifdel = 1")->find();

		if(!$act){
			$this->error("非法操作");
		}else{
			
			$info = $this->m("actinquiry a")->join("company c on a.Acid = c.Id")->where("a.Id = ".$Id." AND a.Ifdel = 1")->field("a.*,c.CompanyName")->find();
			if($_SESSION['UserInfo']['id']){
				if($_SESSION['UserInfo']['cid']){
					
					if($_SESSION['UserInfo']['cid'] == $info['Acid']){
						$state = 2;//不能招标自身
					}
					$cid = $_SESSION['UserInfo']['cid'];
					$CompanyName = $this->m("company")->field("*")->where("Id=".$cid)->find();
					
					$actbj = $this->m("actbaojia")->where("Aid = ".$Id." AND Bcid = ".$_SESSION['UserInfo']['cid'])->find();
					if($actbj){
						$state = 1;
					}
					if($info['ActFiles']) {
						$array = explode('/',$info['ActFiles'] );
						$file_list = array_filter($array);
					} else {
						$file_list = array();
					}
					
					$Actproducts = unserialize($info['Actproduct']);
					$Actproducts = $this->object_array($Actproducts);
					$this->s("state",$state);
					$this->s("file_list",$file_list);
					$this->s("Actproducts",$Actproducts);
					$this->s("CompanyName",$CompanyName);
				}
			}
				
			$buy = $this->m('bid')->where(array('Status' => 1 ))->order('Pubtime DESC')->limit(10)->select();
			$this->s('buy',$buy);
			$this->s("info",$info);
		}
		$this->v();
	}
	public function ActAddDo(){
		if(!isset($_SESSION['UserInfo']['cid'])){
				$this->error("请完善公司信息信息");
		}
		if($_SESSION['UserInfo']['cid'] != $_REQUEST['Bcid']){
				$this->error("非法操作");
		}
		if(!isset($_REQUEST['Aid'])){
			$this->error("非法操作");
		}
		for($i=0;$i<count($_REQUEST['ActName']);$i++){
			$arry[$i]=array(
				'ActName' => $_REQUEST['ActName'][$i],
				'ActNum' => $_REQUEST['ActNum'][$i],
				'Actdj' => $_REQUEST['Actdj'][$i],
				'Actpp' => $_REQUEST['Actpp'][$i],
				'Bpirce' => $_REQUEST['Bpirce'][$i]

			);
		}
		$condition['Bproduct'] = serialize($arry);
		$data = array(
			'Aid' => $_REQUEST['Aid'],
			'Bcid' => $_REQUEST['Bcid'],
			'Bproduct' => $condition['Bproduct'],
			'Bcompany' => $_REQUEST['Bcompany'],
			'Blxr' => $_REQUEST['Blxr'],
			'Btel' => $_REQUEST['Btel'],
			'Bjtime' => time(),
			'Bifdel' => 1
		
		);
		$res = $this->m("actbaojia")->add($data);
		if($res){
			$this->success('报价成功', T('Inquiry/Lists'));
		}else{
			 $this->error('系统繁忙');
		}
		
	}
	    //产品反序列化后转换成数组
    public function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        } if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }
  


//ajax预加载
	public function PageAjax(){
		if(!isset($_REQUEST['page'])){
				$this->error("非法操作");
		}
		$thispage = (int)$_REQUEST['page']*6;
		//$_REQUEST['page'] = 1;
		if($_REQUEST['order']){
			if($_REQUEST['order'] ==1) {
				$ordertype = 'a.Pubtime desc';
			} else{
				$ordertype = 'a.Pubtime asc';
			}
			$this->s("order",$_REQUEST['order']);
		}else{
			$ordertype = 'a.Pubtime desc';
		}
		if($_REQUEST['st']){
			if($_REQUEST['st'] ==1) {
				$endsql = ' AND a.Endtime > '.time();
			} elseif($_REQUEST['st'] ==2){
				$endsql = ' AND a.Endtime <= '.time();
			}else{
				$endsql = 'AND 1=1';
			}
			$this->s("st",$_REQUEST['st']);
		}else{
			$endsql = ' AND 1=1';
		}
		
		$count = count($this->m("actinquiry a")->where("a.Ifdel = 1".$endsql)->select());
		$list = $this->m("actinquiry a")->join("company c on a.Acid = c.Id")->where("Ifdel = 1".$endsql)->field("a.*, c.CompanyName,c.CompanyRegionId")->order($ordertype)->limit($thispage.", 6")->select();
		
		$citys = F("sys_arrcity");
		
		foreach($list as $key=>$v){
			if($v['Endtime']>time()){
				$list[$key]['ifjs'] = 1;
			}else{
				$list[$key]['ifjs'] = 2;
			}
			foreach($citys as $vp){
				foreach($vp['cl'] as $vc){
					if($vc['id'] == $v['CompanyRegionId']){
						$list[$key]['thiscity']=$vp['name'].$vc['name'];
					}
					
				}	
			
			}
			$list[$key]['Pubtime'] = date("Y-m-d",$v['Pubtime']);
		}
		
		$ifhy = ceil($count/6);
		if((int)$_REQUEST['page']>$ifhy){
			$list['ifhy'] = 1;
		}

		echo json_encode($list);
	
	}

	
}